package com.pospi.pai.yunpos.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.mylhyl.circledialog.CircleDialog;
import com.mylhyl.circledialog.params.ProgressParams;
import com.pospi.pai.yunpos.R;
import com.pospi.pai.yunpos.util.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

public class Base2Activity extends SuperBaseActivity {
    private TextView mTitle;
    private ImageView mBack;
    private TextView menu;
    private TextView menu_img;
    private FrameLayout container;
    private static List<Activity> activities = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        mTitle = (TextView) findViewById(R.id.title);
        mBack = (ImageView) findViewById(R.id.back);
        menu = (TextView) findViewById(R.id.menu);
        menu_img = findViewById(R.id.menu_img);
        container = (FrameLayout) findViewById(R.id.container);
        activities.add(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        activities.remove(this);
    }

    public static void killBaseActivity() {
        for (Activity activity : activities) {
            activity.finish();
        }
    }

    /***
     * 设置内容区域
     *
     * @param resId 资源文件ID
     */
    @SuppressWarnings("deprecation")
    public void setContentLayout(int resId) {

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(resId, null);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.FILL_PARENT);
        contentView.setLayoutParams(layoutParams);
        contentView.setBackgroundDrawable(null);
        if (null != container) {
            container.addView(contentView);
        }
    }

    /**
     * 设置中间title的文字
     */
    public void setTitle(String title) {
        mTitle.setText(title);
    }

    /**
     * 设置中间title的文字
     */
    public void setTitle(int title) {
        mTitle.setText(title);
    }

    /**
     * 设置右边按钮的文字
     */
    public void setMenuText(String text) {
        menu.setVisibility(View.VISIBLE);
        menu_img.setVisibility(View.GONE);
        menu.setText(text);
    }

    public void setMenuTextSize(float size) {
        menu.setVisibility(View.VISIBLE);
        menu.setTextSize(size);
    }

    public void setMenuTextColor(int color) {
        menu.setVisibility(View.VISIBLE);
        menu.setTextColor(color);
    }

    /**
     * 设置右边按钮的图片资源
     */
    public void setMenuImage(int resId) {
        if (null != menu) {
            menu.setVisibility(View.GONE);
            menu_img.setVisibility(View.VISIBLE);
            menu_img.setBackgroundResource(resId);
        }
    }

    public TextView getMenu() {
        return menu;
    }

    /**
     * 设置左边后退按钮的隐藏与显示
     */
    public void setBackVisible(boolean visible) {
        if (visible) {
            mBack.setVisibility(View.VISIBLE);
        } else {
            mBack.setVisibility(View.GONE);
        }
    }

    /**
     * 设置右边按钮的点击事件
     */
    public void onMenuClick(View view) {

    }

    /**
     * 设置左边后退按钮的点击事件
     */
    public void onBack(View view) {
        onBackPressed();
    }

    /**
     * 打开一个Activity
     */
    public void startActivity(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }

    public DialogFragment mDialogFragment;

    public void showMyDialog(String msg) {
        mDialogFragment = new CircleDialog.Builder()
                .setProgressText(msg)
                .setProgressStyle(ProgressParams.STYLE_SPINNER)
                .show(getSupportFragmentManager());
    }

    public void closeMyDialog() {
        mDialogFragment.dismiss();
    }

    //支付等待提示
    private LoadingDialog loadingDialog;

    public void showLodingDialog(String msg) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(this);
            loadingDialog.setMessage(msg);
            loadingDialog.show();
        }
    }

    public void showLodingDialog() {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(this);
            loadingDialog.setMessage("请求中...");
            loadingDialog.show();
        }
    }

    //关闭支付等待提示
    public void closeLodindDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }

    public static boolean NumberCheck(String str) {
        String reg = "^[0-9]+(.[0-9]+)?$";
        return str.matches(reg);
    }

    public void printMsg(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new CircleDialog.Builder()
                        .setTitle("提示")
                        .setText(msg)
                        .setWidth(0.5f)
                        .setPositive("确定", null)
                        .show(getSupportFragmentManager());

            }
        });
    }

    public void hintSoftkey() {
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        // 得到InputMethodManager的实例
        if (imm.isActive()) {
            // 如果开启
            imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
                    InputMethodManager.HIDE_NOT_ALWAYS);

        }


    }
    public void hideSoftInput() {
        InputMethodManager inputMethodManager =
                (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(
                this.getWindow().getDecorView().getWindowToken(), 0);
    }
}